R has a number of text processing
functions and packages. In this video, we will introduce you to the Tidyverse package for
handling text, stringr. Before we dive into discussing stringr, I want make sure you are aware of one critical nuance in working
with strings in R. Base R, those functions that are
available without any packages, uses POSIX extended regular expressions. To make things more complicated, stringr uses the ICU
regular expression engine. I found that this is similar enough to JavaScript regular
expressions that you can use tools like RegExr and debuggex
to write your regular expressions. In general, there are three types of string functions that we'll be using
from stringr: detecting matches, extracting matches,
and replacing matches. Detecting matches are
useful when you have an identification task
and simply want to know whether a particular word or
phrase appears in the note. The function str_detect, ask for the column of text to search
and the search string to use. If there is a match, then string detect returns true. If not, it returns false. This is a simple but useful function
for text processing. Extracting matches are useful
when you have an extraction task. I need to actually pull out
the part of the string that matches your regular expression
to use for some other purpose. There are two primary functions
you will use for extraction tasks: string_extract
and string_extract_all. The first function searches and
extracts the first match it finds. The second function continues to search the whole document and extracts
all matches from the document. Both of these functions
have the same arguments. They require the column of text
to search and the string to use. Finally, replacing matches,
searches a text for a match and then replaces all the matching text with
a particular replacement string. This function is useful when
you need to correct things like typos to ensure that your text
processing algorithms work as designed. As with string extractions, there are two primary functions
you will use for string replacement: str_replace,
and str_replace_all. String replace only
replaces the first pattern matched while str_replace_all
replaces all matching patterns. Both functions require the column
of the text to search, the search string to use, and the replacement value. Of note, if you want to use modifiers like case insensitive
or global searching, that is actually fairly easy to do. You just need to use the regex function
around your regular expression, and you can easily set things like
ignore underscore case to true. This is barely scratching the surface of text functions available in stringr, but these will be the ones
you come to again and again. Check out the readings for links
to the RStudio cheat sheets created for stringr to
learn more functions, and have a handy guide to the regular
expression options available.